﻿using System;
using System.Collections.Generic;
using System.Text;

namespace ttbit.form
{
    public class BitmapData
    {
        public int width;
        public int height;
        public byte[] data;
        public bool gray;
        public void LoadRGBA(byte[] stream)
        {
            var result = StbImageSharp.ImageResult.FromMemory(stream, StbImageSharp.ColorComponents.RedGreenBlueAlpha);
            this.data = result.Data;
            this.width = result.Width;
            this.height = result.Height;
            this.gray = false;
        }
        public void LoadGray(byte[] stream)
        {
            var result = StbImageSharp.ImageResult.FromMemory(stream, StbImageSharp.ColorComponents.Grey);
            if (result.Comp != StbImageSharp.ColorComponents.Grey)
            {
                throw new Exception("not a gray data.");
            }
            else
            {
                this.data = result.Data;
            }
            this.width = result.Width;
            this.height = result.Height;
            this.gray = true;
        }
        public void SaveJpg(System.IO.Stream stream, int quality)
        {
            StbImageWriteSharp.ImageWriter w = new StbImageWriteSharp.ImageWriter();
            w.WriteJpg(this.data, this.width, this.height, this.gray ? StbImageWriteSharp.ColorComponents.Grey : StbImageWriteSharp.ColorComponents.RedGreenBlueAlpha, stream, quality);
        }
        public void SavePng(System.IO.Stream stream)
        {
            StbImageWriteSharp.ImageWriter w = new StbImageWriteSharp.ImageWriter();
            w.WritePng(this.data, this.width, this.height, this.gray ? StbImageWriteSharp.ColorComponents.Grey : StbImageWriteSharp.ColorComponents.RedGreenBlueAlpha, stream);
        }
    }
}
